package com.knife.blog.web.action;

import com.knife.blog.domain.PhotoDir;
import com.knife.blog.logic.impl.PhotoServiceImpl;
import com.knife.util.CommUtil;
import com.knife.web.Module;
import com.knife.web.Page;
import com.knife.web.WebForm;
import com.knife.web.tools.IActiveUser;
import com.knife.web.tools.IPageList;

public class BlogPhotoDirAction extends BaseCrudAction {

    private static PhotoServiceImpl photoService = PhotoServiceImpl.getInstance();

    @Override
    public IPageList doQuery(WebForm form, int currentPage, int pageSize) {
        String parentsn = CommUtil.null2String(form.get("parentsn"));
        if (!"".equals(parentsn)) {
        }
        String username = CommUtil.null2String(this.getCurrentUser(form).getUserName());
        return photoService.queryPhotoDir(username,
                currentPage, pageSize);
    }

    @Override
    public Page doAdd(WebForm form, Module module, IActiveUser user) {
        PhotoDir photoDir = (PhotoDir) form2Obj(form);
        photoDir.setInputuser(user.getUserName());
        boolean ret = photoService.addPhotoDir(photoDir);
        if (ret) {
            form.addResult("msg", "添加成功!");
            return super.doQuery(form, module, user);
        } else {
            form.addResult("msg", "添加失败!");
            return module.findPage("edit");
        }
    }

    public Page doManageAdd(WebForm form, Module module) {
        PhotoDir photoDir = (PhotoDir) form2Obj(form);
        boolean ret = photoService.addPhotoDir(photoDir);
        if (ret) {
            form.addResult("msg", "添加成功!");
            return doManageList(form, module);
        } else {
            form.addResult("msg", "添加失败!");
            return module.findPage("edit");
        }
    }

    public Page doManageEdit(WebForm form, Module module) {
        String cid = (String) form.get("cid");
        PhotoDir photoDir = photoService.getPhotoDir(cid);
        form.addPo(photoDir);
        return module.findPage("manageEdit");
    }

    public Page doManageList(WebForm form, Module module) {
        if (!com.knife.blog.logic.BlogAdminConfig.getInstance().isAdmin(
                this.getCurrentUser(form).getUserName())) {
            System.out.println(this.getCurrentUser(form).getUserName());
            return new Page("popedomError", "/manage/norights.htm", "page");
        }
        int currentPage = CommUtil.null2Int(form.get("page"));
        int pageSize = CommUtil.null2Int(form.get("pageSize"));
        if (currentPage < 1) {
            currentPage = 1;
        }
        if (pageSize < 1) {
            pageSize = 15;
        }
        IPageList pageList = photoService.queryPhotoDir(currentPage, pageSize);
        CommUtil.saveIPageList2WebForm(pageList, form);
        return module.findPage("manageList");
    }

    @Override
    public Page doDel(WebForm form, Module module, IActiveUser user) {
        boolean ret = photoService.delPhotoDir((String) form.get("cid"));
        com.knife.blog.logic.HTMLGenerater.savePhotoDirIndexToHTMLByBlog(userService.getUserBlog(user.getUserName()));//生成用户相册首页
        com.knife.blog.logic.HTMLGenerater.saveToHTML(userService.getUserBlog(user.getUserName()));//然后重新生成该用户首页
        form.addResult("msg", ret ? "删除成功!" : "删除失败!");
        return super.doQuery(form, module, user);
    }

    public Page doManageDel(WebForm form, Module module) {
        boolean ret = photoService.delPhotoDir((String) form.get("cid"));
        //com.knife.blog.logic.HTMLGenerater.savePhotoToHTMLByBlog(userService.getUserBlog(user.getUserName()));//然后重新生成该相册首页
        //com.knife.blog.logic.HTMLGenerater.saveToHTML(userService.getUserBlog(user.getUserName()));//然后重新生成该用户首页
        form.addResult("msg", ret ? "删除成功!" : "删除失败!");
        return doManageList(form, module);
    }

    @Override
    public Page doNew(WebForm form, Module module, IActiveUser user) {
        form.addResult("inputuser", user.getUserName());
        return crudPage(form, module, "edit");
    }

    public Page doDelBatch(WebForm form, Module module, IActiveUser user) {
        int ret = photoService.batchDelPhotoDir((String[]) form.get("cid"));
        form.addResult("msg", ret == 0 ? "删除" + ret + "项成功!" : "删除失败!");
        return super.doQuery(form, module, user);
    }

    public Page doManageDelBatch(WebForm form, Module module) {
        int ret = photoService.batchDelPhotoDir((String[]) form.get("cid"));
        form.addResult("msg", ret == 0 ? "删除" + ret + "项成功!" : "删除失败!");
        return doManageList(form, module);
    }

    @Override
    public Page doUpdate(WebForm form, Module module, IActiveUser user) {
        PhotoDir phodir = (PhotoDir) form2Obj(form);
        boolean ret = photoService.updatePhotoDir(phodir);
        com.knife.blog.logic.HTMLGenerater.savePhotoToHTMLByBlog(userService.getUserBlog(phodir.getInputuser()));//然后重新生成该相册首页
        com.knife.blog.logic.HTMLGenerater.saveToHTML(userService.getUserBlog(phodir.getInputuser()));//然后重新生成该用户首页
        form.addResult("msg", ret ? "修改成功!" : "修改失败!");
        return super.doQuery(form, module, user);
    }

    public Page doManageUpdate(WebForm form, Module module) {
        PhotoDir phodir = (PhotoDir) form2Obj(form);
        boolean ret = photoService.updatePhotoDir(phodir);
        com.knife.blog.logic.HTMLGenerater.savePhotoToHTMLByBlog(userService.getUserBlog(phodir.getInputuser()));//然后重新生成该相册首页
        com.knife.blog.logic.HTMLGenerater.saveToHTML(userService.getUserBlog(phodir.getInputuser()));//然后重新生成该用户首页
        form.addResult("msg", ret ? "修改成功!" : "修改失败!");
        return doManageList(form, module);
    }

    /*public Page doUpdate(PhotoDir photoDir){
    boolean ret = photoService.updatePhotoDir(photoDir);
    }

    public Page doDel(String cid){
    boolean ret = photoService.delPhotoDir(cid);
    }*/
    @Override
    public Object form2Obj(WebForm form) {
        String cid = CommUtil.null2String(form.get("cid"));
        PhotoDir obj = photoService.getPhotoDir(cid);
        if (obj == null) {
            obj = new PhotoDir();
        }
        form.toPo(obj);
        return obj;
    }
}
